package com.qyhl.module_practice.team.list;

import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.module_practice.R;
import com.qyhl.module_practice.R2;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionLogUtils;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterPathConstant.PRACTICE_TEAM_LIST)
public class PracticeTeamListActivity extends BaseActivity {
    @BindView(R2.id.title)
    TextView title;

    private Fragment currentFragment;


    @Override
    protected int getContentView() {
        return R.layout.practice_activity_team_list;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {

    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        String volId = getIntent().getStringExtra("volId");
        String instId = getIntent().getStringExtra("instId");
        String streetId = getIntent().getStringExtra("streetId");
        int volStatus = getIntent().getIntExtra("status", 0);
        boolean isStreet = getIntent().getBooleanExtra("isStreet", false);
        boolean isMine = getIntent().getBooleanExtra("isMine", false);


        String titleName = getIntent().getStringExtra("title");
        if (StringUtils.isEmpty(titleName)) {
            title.setText("志愿团体");
        } else {
            title.setText(titleName);
        }

        PracticeTeamListFragment practiceTeamListFragment = PracticeTeamListFragment.newInstance(instId, volId, volStatus, streetId, isStreet, isMine);
        add(practiceTeamListFragment, R.id.fragment_layout, "practice_team_list");


    }

    public void add(Fragment fragment, int id, String tag) {
        FragmentManager mFragmentManager = getSupportFragmentManager();
        FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction();
        Fragment tempFragment = mFragmentManager.findFragmentByTag(tag);
        if (tempFragment != null) {
            fragment = tempFragment;
        }
        if (fragment.isAdded()) {
            addOrShowFragment(mFragmentTransaction, fragment, id, tag);
        } else {
            if (currentFragment != null && currentFragment.isAdded()) {
                mFragmentTransaction.hide(currentFragment).add(id, fragment, tag).commit();
            } else {
                mFragmentTransaction.add(id, fragment, tag).commit();
            }
            currentFragment = fragment;
        }
    }

    /**
     * 添加或者显示 fragment
     *
     * @param fragment 碎片
     */
    private void addOrShowFragment(FragmentTransaction transaction, Fragment fragment, int id, String tag) {
        if (currentFragment == fragment) {
            return;
        }
        if (!fragment.isAdded()) {
            transaction.hide(currentFragment).add(id, fragment, tag).commit();
        } else {
            transaction.hide(currentFragment).show(fragment).commit();
        }
        currentFragment.setUserVisibleHint(false);
        currentFragment = fragment;
        currentFragment.setUserVisibleHint(true);
    }

    @Override
    protected void setListener() {

    }

    @OnClick({R2.id.back_btn})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.back_btn) {
            finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        ActionLogUtils.getInstance().onResume(this, ActionConstant.PRACTICE_TEAM_LIST);
    }

    @Override
    protected void onPause() {
        super.onPause();
        ActionLogUtils.getInstance().onPause(this, ActionConstant.PRACTICE_TEAM_LIST);
    }
}
